package com.hyacinth.lotterysystem.service.impl;

import cn.hutool.core.lang.Validator;
import com.hyacinth.lotterysystem.common.errocde.ServiceErrorCodeConstants;
import com.hyacinth.lotterysystem.common.exception.ServiceException;
import com.hyacinth.lotterysystem.common.utils.EmailUtil;
import com.hyacinth.lotterysystem.service.EmailService;
import jakarta.annotation.Resource;
import org.apache.commons.mail.EmailException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class EmailServiceImpl implements EmailService {
    @Resource
    private EmailUtil emailUtil;
    @Override
    @Async // 异步执行，防止阻塞
    public void sendEmailVerificationCode(String email) throws EmailException {
        if (!Validator.isEmail(email)){
            throw new ServiceException(ServiceErrorCodeConstants.MAIL_ERROR);
        }
        emailUtil.sendEmailVerificationCode(email);
    }
}
